package com.beijing.http.chat;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * @Author Ma Wenqi
 * @Date 2021/3/28 13:57
 * @Version 1.0
 */
public class UdpReceiveDemo03 {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(6666);


        while (true) {

            //准备接收包裹
            byte[] container = new byte[1024];
            DatagramPacket packet = new DatagramPacket(container, 0, container.length);
            socket.receive(packet);//阻塞式接收包裹


            //断开连接 bye
            byte[] data = packet.getData();
            String receiveDatas = new String(data, 0, data.length);

            System.out.println(receiveDatas);

            if (receiveDatas.equals("bye")) {
                break;
            }


        }
        socket.close();

    }
}
